const app=getApp();
let day = new Date();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    tab:[{id:1,tabname:'今日销量'},{id:2,tabname:'昨日销量'},{id:3,tabname:'按日期'}],
    list:[],
    salesTotal:{},
    currPage:1,
    perPage:8,
    total:0,
    status:0,//0：今日销量 3：昨日销量 4：按时间
    type:0,//1：用户 0：商品
    start:day.getFullYear()+"-" + (day.getMonth()+1) + "-" + day.getDate(),
    end:day.getFullYear()+"-" + (day.getMonth()+1) + "-" + day.getDate(),
    isvoer:true,
    loading:true,
    theme:app.theme
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      salesTotal:app.data.shopSales.salesTotal,
      list:app.data.shopSales.countList.rows
    })
   
    // this.shopStatisticList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  onSearch(event){
    this.setData({
      list:[],
      salesTotal:{},
      currPage:1,
      type:event.currentTarget.dataset.type
    },()=>{
      this.setData({
        salesTotal:app.data.shopSales.salesTotal,
        list:app.data.shopSales.countList.rows
      })
      // this.shopStatisticList();
    })
  },
  bindStartChange(e){
    this.setData({
      start:e.detail.value,
      list:[],
      salesTotal:{},
      currPage:1
    },()=>{
      this.setData({
        salesTotal:app.data.shopSales.salesTotal,
        list:app.data.shopSales.countList.rows
      })
      // this.shopStatisticList();
    })
  },
  bindEndChange(e){
    this.setData({
      end:e.detail.value,
      list:[],
      salesTotal:{},
      currPage:1
    },()=>{
      this.setData({
        salesTotal:app.data.shopSales.salesTotal,
        list:app.data.shopSales.countList.rows
      })
      // this.shopStatisticList();
    })
  },
  onChange(event) {
    this.setData({
      active:event.detail.index
    })
    if(event.detail.index==0){
      this.data.status='0';
      this.data.start=day.getFullYear()+"-" + (day.getMonth()+1) + "-" + day.getDate();
    }else if(event.detail.index==1){
      this.data.status=3;
      this.data.start=day.getFullYear()+"-" + (day.getMonth()+1) + "-" + (day.getDate()-1);
    }else if(event.detail.index==2){
      this.data.status=4;
      this.data.start=day.getFullYear()+"-" + day.getMonth() + "-" + day.getDate();
    }
    this.setData({
      loading:true,
      list:[],
      salesTotal:{},
      currPage:1
    },()=>{
      this.setData({
        salesTotal:app.data.shopSales.salesTotal,
        list:app.data.shopSales.countList.rows
      })
      // this.shopStatisticList();
    })
  },
  shopStatisticList(){
    let that=this;
    that.setData({
      loading:true
    })
    let userInfo = app.functions.getLocalStorage('user');
    let params={
      mid:userInfo.id,
      centreId: userInfo.centreId,
      timeType: that.data.status,
      type: that.data.type,
      offset: (that.data.currPage - 1) * that.data.perPage,
      limit: that.data.perPage,
      start: that.data.start,
      end: that.data.end
    };
    app.api.shopStatistic(params,(response)=>{
      that.setData({
        isvoer:true
      })
      const {rows,total}=response.data.countList;
      if(rows.length==0){
        that.setData({
          loading:false
        })
      }
      if(that.data.currPage==1){
        that.setData({
          list:rows,
          salesTotal:response.data.salesTotal
        })
      }else{
        that.setData({
          list:that.data.list.concat(rows)
        })
      }
    },(error)=>{

    })

  },
})